﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _ISYC_303_11
    {
        public static void ISYC_303_11(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 2; //放大系数

            double oal = Convert.ToDouble(data["oal"]) * scale;
            double lpr = Convert.ToDouble(data["lpr"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double dc = Convert.ToDouble(data["dc"]) * scale;
            double dn = Convert.ToDouble(data["dn"]) * scale;
            double tp = Convert.ToDouble(data["tp"]) * scale;
            double sig = Convert.ToDouble(data["sig"]);
            double lu = Convert.ToDouble(data["lu"]) * scale;


            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + oal + 4, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 2, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dconms / 2 - 0.4);
            Vector2 v5 = new Vector2(v4.X + 0.4, v4.Y + 0.4);
            Vector2 v6 = new Vector2(v3.X + ls, v5.Y);
            Vector2 v7 = new Vector2(v6.X + 0.6, v3.Y + dn / 2);
            Vector2 v8 = new Vector2(v7.X, v7.Y * -1);
            Vector2 v9 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v10 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v11 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v12 = new Vector2(v3.X + oal, v3.Y);
            Vector2 v13 = new Vector2(v12.X - dc / 2 / Math.Tan(sig / 2 * pi / 180), v12.Y + dc / 2);
            Vector2 v14 = new Vector2(v13.X - lu / 21.336, v13.Y);
            Vector2 v15 = new Vector2(v14.X - tp / 2, v14.Y - dc / 11.629);
            Vector2 v16 = new Vector2(v14.X - tp, v14.Y);
            Vector2 v17 = new Vector2(v15.X - tp, v15.Y);
            Vector2 v18 = new Vector2(v16.X - tp, v16.Y);
            Vector2 v19 = new Vector2(v17.X - tp, v17.Y);
            Vector2 v20 = new Vector2(v18.X - tp, v18.Y);
            Vector2 v21 = new Vector2(v19.X - tp, v19.Y);
            Vector2 v22 = new Vector2(v20.X - tp, v20.Y);
            Vector2 v23 = new Vector2(v21.X - tp, v21.Y);
            Vector2 v24 = new Vector2(v22.X - tp, v22.Y);
            Vector2 v25 = new Vector2(v23.X - tp, v23.Y);
            Vector2 v26 = new Vector2(v24.X - tp, v24.Y);
            Vector2 v27 = new Vector2(v25.X - tp, v12.Y + dn / 2);
            Vector2 v28 = new Vector2(v13.X, v13.Y * -1);
            Vector2 v29 = new Vector2(v14.X, v14.Y * -1);
            Vector2 v30 = new Vector2(v15.X, v15.Y * -1);
            Vector2 v31 = new Vector2(v16.X, v16.Y * -1);
            Vector2 v32 = new Vector2(v17.X, v17.Y * -1);
            Vector2 v33 = new Vector2(v18.X, v18.Y * -1);
            Vector2 v34 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v35 = new Vector2(v20.X, v20.Y * -1);
            Vector2 v36 = new Vector2(v21.X, v21.Y * -1);
            Vector2 v37 = new Vector2(v22.X, v22.Y * -1);
            Vector2 v38 = new Vector2(v23.X, v23.Y * -1);
            Vector2 v39 = new Vector2(v24.X, v24.Y * -1);
            Vector2 v40 = new Vector2(v25.X, v25.Y * -1);
            Vector2 v41 = new Vector2(v26.X, v26.Y * -1);
            Vector2 v42 = new Vector2(v27.X, v27.Y * -1);
            Vector2 v43 = new Vector2(v29.X, v29.Y + dc / 7.357);
            Vector2 v44 = new Vector2(v30.X, v30.Y + dc / 7.357);
            Vector2 v45 = new Vector2(v31.X, v31.Y + dc / 7.357);
            Vector2 v46 = new Vector2(v32.X, v32.Y + dc / 7.357);
            Vector2 v47 = new Vector2(v33.X, v33.Y + dc / 7.357);
            Vector2 v48 = new Vector2(v34.X, v34.Y + dc / 7.357);
            Vector2 v49 = new Vector2(v35.X, v35.Y + dc / 7.357);
            Vector2 v50 = new Vector2(v36.X, v36.Y + dc / 7.357);
            Vector2 v51 = new Vector2(v37.X, v37.Y + dc / 7.357);
            Vector2 v52 = new Vector2(v38.X, v38.Y + dc / 7.357);
            Vector2 v53 = new Vector2(v39.X, v39.Y + dc / 7.357);
            Vector2 v54 = new Vector2(v40.X, v40.Y + dc / 7.357);
            Vector2 v55 = new Vector2(v41.X, v41.Y + dc / 7.357);
            Vector2 v56 = new Vector2(v12.X - lpr, v12.Y);
            Vector2 v57 = new Vector2(v56.X, v56.Y + dconms / 2 + 1);
            Vector2 v58 = new Vector2(v56.X, v56.Y - dconms / 2 - 1);
            Vector2 v59 = new Vector2(v14.X, v12.Y);
            Vector2 v60 = fun.Polar(v59, -176 * pi / 180, lu / 1.778);
            Vector2 v61 = new Vector2(v60.X + lu / 12.353, v42.Y);
            Vector2 v62 = new Vector2(v59.X, v59.Y + dc / 3.755);
            Vector2 v63 = new Vector2(v15.X, v62.Y - dc / 23.258);
            Vector2 v64 = new Vector2(v16.X, v62.Y);
            Vector2 v65 = new Vector2(v17.X, v63.Y);
            Vector2 v66 = new Vector2(v18.X, v64.Y);
            Vector2 v67 = new Vector2(v19.X, v65.Y);
            Vector2 v68 = new Vector2(v20.X, v62.Y);
            Vector2 v69 = new Vector2(v21.X, v63.Y);
            Vector2 v70 = new Vector2(v22.X, v62.Y);
            Vector2 v71 = new Vector2(v23.X, v63.Y);
            Vector2 v72 = new Vector2(v24.X, v62.Y);
            Vector2 v73 = new Vector2(v25.X, v63.Y);
            Vector2 v74 = new Vector2(v26.X, v62.Y);
            Vector2 v75 = new Vector2(v27.X, v63.Y);
            Vector2 v76 = new Vector2(v75.X - lu / 7.62, v75.Y);
            Vector2 v77 = new Vector2(v60.X, v27.Y);
            Vector2 v78 = fun.Polar(v59, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v79 = fun.Polar(v78, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v80 = fun.Polar(v79, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v81 = fun.Polar(v80, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v82 = fun.Polar(v81, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v83 = fun.Polar(v82, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v84 = fun.Polar(v83, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v85 = fun.Polar(v84, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v86 = fun.Polar(v85, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v87 = fun.Polar(v86, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v88 = fun.Polar(v87, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v89 = fun.Polar(v88, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));
            Vector2 v90 = fun.Polar(v89, -176 * pi / 180, tp / 2 / Math.Cos(4 * pi / 180));



            //作图
            //1
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            Line line2 = new Line(v57, v58);
            line2.Layer = centre;
            line2.Linetype = Linetype.DashDot;
            line2.LinetypeScale = 3;
            dxf.AddEntity(line2);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v5));
            line3.Vertexes.Add(new LwPolylineVertex(v10));
            line3.Vertexes.Add(new LwPolylineVertex(v11));
            line3.Vertexes.Add(new LwPolylineVertex(v4));
            line3.Vertexes.Add(new LwPolylineVertex(v5));
            line3.Vertexes.Add(new LwPolylineVertex(v6));
            line3.Vertexes.Add(new LwPolylineVertex(v9));
            line3.Vertexes.Add(new LwPolylineVertex(v10));
            fun.PolyFill(dxf, line3, 0.1, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v6));
            line4.Vertexes.Add(new LwPolylineVertex(v7));
            line4.Vertexes.Add(new LwPolylineVertex(v8));
            line4.Vertexes.Add(new LwPolylineVertex(v9));
            fun.PolyFill(dxf, line4, 0.1, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v7));
            line5.Vertexes.Add(new LwPolylineVertex(v27));
            line5.Vertexes.Add(new LwPolylineVertex(v26));
            line5.Vertexes.Add(new LwPolylineVertex(v25));
            line5.Vertexes.Add(new LwPolylineVertex(v24));
            line5.Vertexes.Add(new LwPolylineVertex(v23));
            line5.Vertexes.Add(new LwPolylineVertex(v22));
            line5.Vertexes.Add(new LwPolylineVertex(v21));
            line5.Vertexes.Add(new LwPolylineVertex(v20));
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            line5.Vertexes.Add(new LwPolylineVertex(v18));
            line5.Vertexes.Add(new LwPolylineVertex(v17));
            line5.Vertexes.Add(new LwPolylineVertex(v16));
            line5.Vertexes.Add(new LwPolylineVertex(v15));
            line5.Vertexes.Add(new LwPolylineVertex(v14));
            line5.Vertexes.Add(new LwPolylineVertex(v13));
            line5.Vertexes.Add(new LwPolylineVertex(v12));
            fun.PolyFill(dxf, line5, 0.1, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v12));
            line6.Vertexes.Add(new LwPolylineVertex(v28));
            line6.Vertexes.Add(new LwPolylineVertex(v29));
            line6.Vertexes.Add(new LwPolylineVertex(v30));
            line6.Vertexes.Add(new LwPolylineVertex(v31));
            line6.Vertexes.Add(new LwPolylineVertex(v32));
            line6.Vertexes.Add(new LwPolylineVertex(v33));
            line6.Vertexes.Add(new LwPolylineVertex(v34));
            line6.Vertexes.Add(new LwPolylineVertex(v35));
            line6.Vertexes.Add(new LwPolylineVertex(v36));
            line6.Vertexes.Add(new LwPolylineVertex(v37));
            line6.Vertexes.Add(new LwPolylineVertex(v38));
            line6.Vertexes.Add(new LwPolylineVertex(v39));
            line6.Vertexes.Add(new LwPolylineVertex(v40));
            line6.Vertexes.Add(new LwPolylineVertex(v41));
            line6.Vertexes.Add(new LwPolylineVertex(v42));
            line6.Vertexes.Add(new LwPolylineVertex(v8));
            fun.PolyFill(dxf, line6, 0.1, contour);

            //标注

            double height = 2;
            double arrowsize = 2;
            double exline = 2;

           
            fun.dimrot1(dxf, v18, v20, 0, oal / 15, 1, height, arrowsize, exline, "TP", "", "");

            fun.dimrot1(dxf, v57, v12, 0, oal / 4, 1, height, arrowsize, exline, "LPR", "", "");

            fun.dimrot1(dxf, v4, v12, 0, oal / 2.5, 1, height, arrowsize, exline, "OAL", "", "");

            fun.dimrot1(dxf, v5, v10, 90, dconms / 2, 1, height, arrowsize, exline, "DCONMS", "", "");

            fun.dimrot1(dxf, v28, v13, -90, dconms * 3 / 2, 1, height, arrowsize, exline, "DCD", "", "");

            fun.dimrot1(dxf, v7, v8, -90, dconms / 2, 1, height, arrowsize, exline, "DN", "", "");

            fun.dimrot1(dxf, v11, v9, 180, oal / 10, 1, height, arrowsize, exline, "LS", "", "");

            fun.dimrot1(dxf, v42, v29, 180, oal / 10, 1, height, arrowsize, exline, "APMX", "", "");

            fun.dimrot1(dxf, v42, v12, 180, oal / 3.5, 1, height, arrowsize, exline, "LH", "", "");

            fun.jdbz2(dxf, new Line(v12, fun.Polar(v12, -sig / 2 * pi / 180, 0.1)), new Line(v12, fun.Polar(v12, sig / 2 * pi / 180, 0.1)), dconms, height, arrowsize, exline, "SIG", "", "");



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }




            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}